<template>
    <div class="view-main">
        <div class="view-header">
            <el-radio-group v-model="timeSelect" @change="queryTimeChanged">
                <el-radio-button label="全部"></el-radio-button>
                <el-radio-button label="今日"></el-radio-button>
                <el-radio-button label="本周"></el-radio-button>
                <el-radio-button label="本月"></el-radio-button>
                <el-radio-button label="自定义"></el-radio-button>
            </el-radio-group>
            <el-date-picker v-if="timeShow" v-model="timeRange" type="datetimerange" format="yyyy-MM-dd HH:mm:ss"
                            value-format="yyyy-MM-dd HH:mm:ss" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
                            align="right" @change="handleTimeRangeChanged">
            </el-date-picker>
            <el-input placeholder="案卷名称" v-model="queryParam.name" style="width: 200px"></el-input>
            <el-input placeholder="案卷编号" v-model="queryParam.code" style="width: 200px" ></el-input>

            <span>
        <el-button type="primary" @click="getDataList()" icon="el-icon-search">查询</el-button>

      </span>
        </div>
        <div class="view-body">
            <div class="light-table">
                <el-table :data="tableData" height="300" highlight-current-row style="width: 100%">
                    <el-table-column property="name" label="卷宗名称" align="center" show-overflow-tooltip></el-table-column>
                    <el-table-column property="code" label="卷宗编号" align="center" show-overflow-tooltip></el-table-column>
                    <el-table-column property="tempBackMan" label="归还人" align="center" show-overflow-tooltip></el-table-column>
                    <el-table-column property="tempBackTime" label="归还时间" align="center" show-overflow-tooltip></el-table-column>
                    <el-table-column label="操作" align="center" width="330">
                        <template slot-scope="scope">
                            <el-button type="text" size="mini" @click="handleDetail(scope.row)">详情</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination background layout="total, sizes, prev, pager, next, jumper" :page-sizes="[10, 20, 50, 100]"
                               :total="total" :current-page="pageNum" :page-size.sync="pageSize" :page-count="pages"
                               @current-change="handlePageChange" @size-change="handleSizeChange"></el-pagination>
            </div>
        </div>


        <!--入库,编辑-->
        <el-dialog class="no-bottom-padding user-dialog" :title="typeText" :visible.sync="dialogVisible" width="480px" :close-on-click-modal="false">
            <el-form :model="addForm" ref="addForm" label-width="100px" size="mini" style="padding-right: 20px" :disabled="type_dia === 2">
                <!--选择框-->
                <el-form-item label="案件名称" prop="name">
                    <el-input v-model.number="addForm.name" clearable disabled></el-input>

                </el-form-item>

                <!--案卷名称关联，不可改-->
                <el-form-item label="案卷编号" prop="code" >
                    <el-input v-model.number="addForm.code" clearable disabled></el-input>
                </el-form-item>

                <!--案卷名称关联，不可改-->
                <el-form-item label="案卷类型" prop="zjType">
                    <el-input v-model.number="addForm.ajType" clearable disabled></el-input>
                </el-form-item>

                <el-form-item label="卷宗类型" prop="jzType">
                    <el-select v-model="addForm.jzType" filterable placeholder="请选择" >
                        <el-option
                                v-for="item in jzList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="登记人" prop="tempApplyMan">
                    <el-input v-model.number="addForm.tempApplyMan" clearable disabled></el-input>

                </el-form-item>

                <!--需要关联登记人,所以每个人注册时需要加入登记单位,或者学习财务系统,改为申请单位-->
                <el-form-item label="申请单位"  prop="applyUnit">
                    <el-input v-model.number="addForm.applyUnit" clearable disabled></el-input>
                </el-form-item>

                <el-form-item label="登记时间"  prop="registDate">
                    <el-date-picker type="date"  v-model="addForm.registDate" style="width: 100%;"
                                    value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
                </el-form-item>

                <el-form-item label="存放位置"  prop="position">
                    <el-input v-model.number="addForm.position" clearable></el-input>
                </el-form-item>

                <el-form-item label="卷宗描述"  prop="description">
                    <el-input v-model.number="addForm.description" clearable></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer" v-if="type_dia < 2">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="updateConfirm('addForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import { isEmpty, formatDate, stringFormat,getStartTimeOfDate, getEndTimeOfDate, getStartTimeOfWeek, getEndTimeOfWeek, getStartTimeOfMonth, getEndTimeOfMonth } from '../../utils/util'
    // import { checkLoginName, checkPhone, checkEmail, checkObj, isEmpty, stringFormat } from '../../utils/util'

    import { getAction } from '@/utils/manage'

    export default {
        name: 'fileRecord',
        data() {
            return {
                outDialogVisible:false,
                dialogVisible: false,
                type_dia: 0,
                timeSelect: '全部',
                queryParam: {
                    name: '',
                    code: '',
                    startLogTime:'',
                    endLogTime:'',
                },
                jzList: [
                    { value: "卷宗类型1", label: "卷宗类型1" },
                    { value: "卷宗类型2", label: "卷宗类型2" },
                    { value: "卷宗类型3", label: "卷宗类型3" }
                ],
                addForm: {
                    //
                    value:'',
                    name: '',
                    code: '',
                    ajType:'',
                    jzType:'',
                    registDate:'',
                    registrant:'',
                    status:'',
                    position:'',
                    applyUnit:'',
                    description:''
                },
                //临时出库
                tempOutForm:{
                    //临时出库申请人
                    registrant:'',
                    tempApplyUnit:'',
                    tempRecipient:'',
                    tempRecUnit:'',
                    tempApplyTime:'',
                    tempPreBackTime:'',
                    tempReason:'',
                    name: '',
                    ajType:'',
                    code: '',
                    jzType:'',
                    position:'',
                    status:''
                },
                tempOutRules:{
                    registrant: [{ required: true, message: '请选择申请人', trigger: 'blur' }],
                    tempApplyUnit: [{ required: true, message: '请选择申请单位', trigger: 'blur' }],
                    tempRecipient: [{ required: true, message: '请输入接收人', trigger: 'blur' }],
                    tempRecUnit: [{ required: true, message: '请选择接收单位', trigger: 'blur' }],
                    tempApplyTime: [{ required: true, message: '请选择申请时间', trigger: 'blur' }],
                    tempReason: [{ required: true, message: '请输入申请事由', trigger: 'blur' }],

                },
                timeRange: [],
                timeShow: false,
                // tableData: {},
                tableData: [],
                pageNum: 1,
                pageSize: 10,
                pages: 0,
                total: 0,
                url: {
                    ajPage: '/aj/record/allType',
                    page: '/aj/jz-manage',
                    pageOfOut: '/aj/jz-manage/out',
                    pageOfBack: '/aj/jz-manage/back',
                    // insert:'/aj/jz-manage/insert',
                    // update:'/aj/jz-manage//update/{0}',
                    // delete:'/aj/jz-manage//delete/{0}',

                    // pageLog: '/sys/log',
                    // export: '/sys/log/export'
                }
            }
        },
        created() {
            this.queryTimeChanged(this.timeSelect)
            this.getDataList()},
        computed: {
            typeText() {
                if (this.type_dia === 1){
                    return '修改'
                }else if (this.type_dia === 0){
                    return '入库'
                }else if (this.type_dia === 2){
                    return '详情'
                }else if (this.type_dia === 3){
                    return '临时出库'
                }
            }
        },

        methods: {
            handleDetail(row) {
                this.addForm = JSON.parse(JSON.stringify(row))
                this.type_dia = 2
                this.dialogVisible = true
                if (!isEmpty(this.$refs.addForm)) {
                    this.$refs.addForm.resetFields()
                }
            },
            queryTimeChanged(text) {
                let now = new Date()
                switch (text) {
                    case '今日':
                        this.timeShow = false
                        var startTime = getStartTimeOfDate(now)
                        var endTime = getEndTimeOfDate(now)
                        console.log('startTime=', startTime)
                        this.queryParam.startLogTime = formatDate(startTime, 'yyyy-MM-dd hh:mm:ss')
                        this.queryParam.endLogTime = formatDate(endTime, 'yyyy-MM-dd hh:mm:ss')
                        break
                    case '本周':
                        this.timeShow = false
                        var startTime = getStartTimeOfWeek(now)
                        var endTime = getEndTimeOfWeek(now)
                        this.queryParam.startLogTime = formatDate(startTime, 'yyyy-MM-dd hh:mm:ss')
                        this.queryParam.endLogTime = formatDate(endTime, 'yyyy-MM-dd hh:mm:ss')
                        break
                    case '本月':
                        this.timeShow = false
                        var startTime = getStartTimeOfMonth(now)
                        var endTime = getEndTimeOfMonth(now)
                        this.queryParam.startLogTime = formatDate(startTime, 'yyyy-MM-dd hh:mm:ss')
                        this.queryParam.endLogTime = formatDate(endTime, 'yyyy-MM-dd hh:mm:ss')
                        break
                    case '自定义':
                        this.timeShow = true
                        this.queryParam.startLogTime = ''
                        this.queryParam.endLogTime = ''
                        break
                    default:
                        this.timeShow = false
                        this.queryParam.startLogTime = ''
                        this.queryParam.endLogTime = ''
                        break
                }
            },
            handleTimeRangeChanged(text) {
                this.queryParam.startLogTime = text[0]
                this.queryParam.endLogTime = text[1]
            },
            // searchReset() {
            //     this.queryParam = { time: '全部', timeRange: {} }
            // },
            //
            updateConfirm(){
                this.$refs.addForm.validate(valid => {
                    if (valid) {
                        let _form = JSON.parse(JSON.stringify(this.addForm))
                        // console.log("11111")
                        // console.log(_form)
                        //this.type_dia == 0 入库 insert
                        if (this.type_dia === 0){
                            var address = this.url.insert
                            this.$http.put(address, _form).then(() => {
                                this.getDataList()
                            })
                        }
                        //this.type_dia == 1 修改
                        else if (this.type_dia === 1){
                            var address = stringFormat(this.url.update, [_form.id])
                            this.$http.put(address, _form).then(() => {
                                this.getDataList()
                            })
                        }
                        this.dialogVisible = false
                    } else {
                        return false
                    }
                })
            },
            getDataList() {
                let params = { _pageSize: this.pageSize, _pageNum: this.pageNum, ...this.queryParam }

                this.$http.get(this.url.pageOfBack, {params}).then(data => {
                    let result = data.result
                    this.tableData = result.records
                    this.pageNum = result.current
                    this.pageSize = result.size
                    this.pages = result.pages
                    this.total = result.total
                })
            },
            handlePageChange(page) {
                this.pageNum = page
                this.getDataList()
            },
            handleSizeChange(size) {
                this.pageSize = size
                this.getDataList()
            }
        }
    }
</script>
